package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import pojo.Manager;
import pojo.User;
import service.LoginService;

import java.util.List;

@Controller
@RequestMapping(value = "/hotel")
public class LoginController {
    @Autowired
    @Qualifier("LoginServiceImpl")
    private LoginService loginService;

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    //登录验证
    @RequestMapping("/login")
    public String login(@RequestParam("account") String account, @RequestParam("password") String password, Model model) {
        User user = new User();
        user.setAccount(account);
        user.setPassword(password);
        user.setNum(1);
        if (loginService.login(user) != null) {
            System.out.println("1");
            return "redirect:/hotel/main";     //进入登录界面
        } else {
            //如果查不到数据 则登录失败
            model.addAttribute("error", "账号或密码错误");
            return "login";
        }
    }

    //修改登录密码
    @RequestMapping("/toUpdatePassword")
    public String toUpdate() {
        return "updatePassword";
    }

    @RequestMapping("/updatePassword")
    public String updatePassword(@RequestParam("account") String account, @RequestParam("password") String password
            , @RequestParam("newPassword") String newPassword, Model model) {
        User user = new User();
        user.setAccount(account);
        user.setPassword(password);
        user.setNum(1);
        if (loginService.login(user) != null) {
            User newUser = new User();
            newUser.setNum(1);
            newUser.setAccount(account);
            newUser.setPassword(newPassword);
            loginService.updatePassword(newUser);
            return "redirect:/hotel/main";
        } else {
            return "redirect:/hotel/updatePasswordError";
        }
    }

    //修改密码错误
    @RequestMapping("/updatePasswordError")
    public String updateError() {
        return "updatePasswordError";
    }

    //注销账户
    @RequestMapping("/toCancelUser")
    public String toCancelUser(Model model) {
        List<User> list = loginService.queryUser();
        model.addAttribute("list", list);
        return "cancelUser";
    }

    @RequestMapping("/cancelUser")
    public String cancelUser(@RequestParam("account") String account, Model model) {
        if (account != null) {
            User user = new User();
            user.setAccount(account);
            loginService.cancelUser(user);
            Manager manager=new Manager();
            manager.setAccount(account);
            loginService.deleteManager(manager);
        }
        return "redirect:/hotel/main";
    }

    //激活账户
    @RequestMapping("/toactiveUser")
    public String toactiveUser(Model model) {
        List<User> list = loginService.queryUser();
        model.addAttribute("list", list);
        return "activeUser";
    }

    @RequestMapping("/activeUser")
    public String activeUser(@RequestParam("account") String account, Model model) {
        if (account != null) {
            User user = new User();
            user.setAccount(account);
            loginService.activeUser(user);
        }
        return "redirect:/hotel/main";
    }

    //添加登录账户
    @RequestMapping("/toaddUser")
    public String toaddUser() {
        return "addUser";
    }

    @RequestMapping("/addUser")
    public String addUser(@RequestParam("account") String account, @RequestParam("password") String password,
                          @RequestParam("num") int num) {
        if (account != null && password != null && (num == 1 | num == 2)) {
            User user = new User();
            user.setAccount(account);
            user.setPassword(password);
            user.setNum(num);
            loginService.addUser(user);
            Manager manager=new Manager();
            manager.setAccount(account);
            manager.setPassword(password);
            manager.setName(account);
            loginService.addManager(manager);
            return "redirect:/hotel/main";
        } else {
            return "redirect:/hotel/error";
        }

    }

    //查看酒店管理员信息
    @RequestMapping("/findManager")
    public String findManager(Model model){
        List<Manager> list=loginService.queryManager();
        model.addAttribute("list",list);
        return "findManager";
    }
    //修改管理员信息
    @RequestMapping("/toUpdateManager")
    public String toUpdateManager(Model model){
        List<Manager> list=loginService.queryManager();
        model.addAttribute("list",list);
        return "updateManager";
    }
    @RequestMapping("/updateManager")
    public String updateManager(@RequestParam("account")String account,@RequestParam("card_id")String card_id,
                                @RequestParam("name")String name,@RequestParam("sex")String sex,@RequestParam("telephone")
                                String telephone){
        Manager manager=new Manager();
        if(account!=null&&!account.equals("")){
            manager.setAccount(account);
            if(card_id!=null&&!card_id.equals("")){
                manager.setCard_id(card_id);
            }
            if(sex!=null&&!sex.equals("")){
                manager.setSex(sex);
            }
            if(name!=null&&!name.equals("")){
                manager.setName(name);
            }
            if(telephone!=null&&!telephone.equals("")){
                manager.setTelephone(telephone);
            }
            loginService.updateManager(manager);
            return "redirect:/hotel/main";
        }else{
            return "error";
        }

    }


}
